﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;

namespace TireService.Web.UserControls
{
    public partial class ucEditTireSetStatus : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void callbackEditTireSetStatusPanel_Callback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            LoadUserControl(Convert.ToInt32(e.Parameter));
        }

        /// <summary>
        /// Public function for loading usercontrol
        /// </summary>
        public void LoadUserControl(int TireSetId)
        {
            // Initiate datalayer
            TireServiceDB db = new TireServiceDB();


            if (TireSetId != 0)
            {
                LoadTireSet(TireSetId, db);
            }
            else
            {
                // Error
                throw new ArgumentException("Hjulsæt ikke fundet");
            }
        }

        /// <summary>
        /// Function that load tireset and set edit form values
        /// </summary>
        /// <param name="TireSetId">Id on tireset to loadTireSet</param>
        /// <param name="db">Connection to database</param>
        private void LoadTireSet(int TireSetId, TireServiceDB db)
        {
            // Fetch tiresetId
            TireSet tireset = db.GetTireSet(TireSetId);

            // Set values in edit form
            if (tireset != null)
            {
                // Get all tireset status combinations
                List<TireSetStatus> tiresetstatuslist = db.GetTireSetStatus();

                // Get current tireset status
                TireSetStatus currentstatus = db.GetTireSetStatus(tireset.TireSetStatusId);

                // Remove current status in list
                tiresetstatuslist.Remove(currentstatus);

                // Bind possible new status options to dropdownlist
                ddlNewTireSetStatus.DataSource = tiresetstatuslist;
                ddlNewTireSetStatus.TextField = "Description";
                ddlNewTireSetStatus.ValueField = "TireSetStatusId";
                ddlNewTireSetStatus.DataBind();

                // Show current tireset status description
                lblCurrentTireSetStatusValue.Text = db.GetTireSetStatusDescription(tireset.TireSetStatusId);

                //Shared.TireSetStatus.AwaitsMoveToStock
                // action: 
                // validate: not from mounted and inactive state

                //Shared.TireSetStatus.InActive
                // action: select tireset to replace with or error

                //Shared.TireSetStatus.MountedOnCar
                // action: switch around on two tireset (ask user to approve)
                // validate: Not from inactive state

                //Shared.TireSetStatus.OnStock
                // action: if switch around on two tireset (ask user to approve

                //Shared.TireSetStatus.PackedForService
                //Shared.TireSetStatus.PlacedAtCustomer
                //Shared.TireSetStatus.ReturnedToCustomer
                //Shared.TireSetStatus.StockedAtCustomer
            }
            else
            {
                // Error
                throw new ArgumentException("Hjulsæt kunne ikke loades");
            }
        }

        ///// <summary>
        ///// Callback function that saves tireset
        ///// </summary>
        //protected void callBackSaveTireSetPattern_Callback(object sender, CallbackEventArgs e)
        //{
        //    try
        //    {
        //        // Get values from client
        //        string[] param = e.Parameter.Split('|');

        //        int TireSetId = Convert.ToInt32(param[0]);

        //        // Initiate datalayer
        //        TireServiceDB db = new TireServiceDB();

        //        TireSet tireset = new TireSet();

        //        if (TireSetId == 0)
        //        {
        //            // Error
        //            throw new ArgumentException("Hjulsæt ikke fundet");
        //        }
        //        else
        //        {
        //            // Load tireset
        //            tireset = db.GetTireSet(TireSetId);
        //        }

        //        tireset.LeftFrontTirePattern = Convert.ToDecimal(param[1]);
        //        tireset.RightFrontTirePattern = Convert.ToDecimal(param[2]);
        //        tireset.LeftRearTirePattern = Convert.ToDecimal(param[3]);
        //        tireset.RightRearTirePattern = Convert.ToDecimal(param[4]);

        //        // Get UserId
        //        Guid UserId = new Guid(Membership.GetUser().ProviderUserKey.ToString());

        //        // Create tireset log
        //        TireSetLogBLL tiresetBLL = new TireSetLogBLL();
        //        tiresetBLL.CreateTireSetLog(db, tireset, DateTime.Now, tireset.TireSetStatusId, tireset.TireSetStatusId, UserId, "Opdatering af dækmønster", tireset.LeftFrontTirePattern, tireset.RightFrontTirePattern, tireset.LeftRearTirePattern, tireset.RightRearTirePattern, tireset.ContainerCargoRoomId);

        //        // Commit changes to database
        //        db.SubmitChanges();

        //        // Check if email should be send to customer
        //        if (Convert.ToBoolean(param[5]))
        //        {
        //            // Send email to customer
        //            EmailNotificationServices emailService = new EmailNotificationServices();

        //            Car car = db.GetCar(tireset.CarId);

        //            // Send email to customer with tire set pattern witth user on bcc
        //            emailService.TireSetPattern_SendCustomerEmail(car.CustomerId, db.GetCustomerEmail(car.CustomerId), db.GetCustomerName(car.CustomerId), car.RegistrationNumber, tireset, db);
        //        }

        //        // Dispose database connection
        //        db.Dispose();
        //    }
        //    catch (Exception ex)
        //    {
        //        // Error
        //        throw new ArgumentException("Fejl opstod: " + ex.Message);
        //    }
        //}

    }
}